<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改数据库信息')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-sbrDatabase-edit" th:object="${sbrDatabase}">
            <input name="id" th:field="*{id}" type="hidden">
            <input type="hidden" name="validFlag" id="validFlag" th:field="*{validFlag}">
            <div class="form-group">    
                <label class="col-sm-2 control-label is-required">数据库名称：</label>
                <div class="col-sm-4">
                    <input name="databaseName" th:field="*{databaseName}" class="form-control" type="text" required>
                </div>
                <label class="col-sm-2 control-label is-required">数据库版本：</label>
                <div class="col-sm-4">
                    <select name="driverType" class="form-control m-b" th:with="type=${@dict.getType('sbr_driver_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{driverType}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-2 control-label is-required">数据库地址：</label>
                <div class="col-sm-4">
                    <input name="host" th:field="*{host}" class="form-control" type="text" required>
                </div>
                <label class="col-sm-2 control-label is-required">端口号：</label>
                <div class="col-sm-4">
                    <input name="port" th:field="*{port}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label is-required">用户名：</label>
                <div class="col-sm-4">
                    <input name="userName" th:field="*{userName}" class="form-control" type="text">
                </div>
                <label class="col-sm-2 control-label is-required">密码：</label>
                <div class="col-sm-4">
                    <input name="password" th:field="*{password}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label"></label>
                <div class="col-sm-4">
                    <a class="btn btn-primary single" onclick="testConn()">
                        <i class="fa fa-edit"></i> 测试连接
                    </a>
                </div>
                <label class="col-sm-2 control-label"></label>
                <div class="col-sm-4" id="testConnMsg">
                    <span class="badge">未测试</span>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "sqlbuilder/sbrDatabase";
        $("#form-sbrDatabase-edit").validate({
            focusCleanup: true
        });

        if ($("#validFlag").val() == '1') {
            $("#testConnMsg").html('<span class="badge badge-success">连接成功</span>');
        } else {
            $("#testConnMsg").html('<span class="badge label-warning">连接失败</span>');
        }

        function submitHandler() {
            if ($.validate.form()) {
                var validFlag = $("#validFlag").val();
                if (validFlag == '') {
                    $.modal.alertError('未测试数据连接！');
                    return;
                }
                if (validFlag == '0') {
                    $.modal.confirm("当前数据库连接未测试成功,保存后无法正常使用?", function() {
                        $.operate.save(prefix + "/edit", $('#form-sbrDatabase-edit').serialize());
                    });
                } else {
                    $.operate.save(prefix + "/edit", $('#form-sbrDatabase-edit').serialize());
                }
            }
        }

        function testConn() {
            $.modal.loading("数据库测试连接中...");
            $.ajax({
                type: "post",
                url: prefix + "/testConn",
                data: $('#form-sbrDatabase-edit').serialize(),
                success: function(data) {
                    if (data.code == 0) {
                        $.modal.msgSuccess('连接成功!');
                        $("#validFlag").val(1);
                        $("#testConnMsg").html('<span class="badge badge-success">连接成功</span>');
                    } else {
                        $.modal.msgError('连接失败, 请检查!');
                        $("#validFlag").val(0);
                        $("#testConnMsg").html('<span class="badge label-warning">连接失败</span>');
                    }
                    $.modal.closeLoading();
                }
            });
        }
    </script>
</body>
</html>